package com.hibase.core.rule.web.ruledatasource;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.rule.entity.ruledatasource.RuleDataSource;
import com.hibase.core.rule.entity.ruledatasource.vo.*;
import com.hibase.core.rule.service.ruledatasource.RuleDataSourceService;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述:
 *
 * @author li-wei-hua
 * @date 2020-03-23 14:38
 */
@RestController
@RequestMapping("/rule-data-source")
@Api(tags = "动态规则数据源配置")
public class RuleDataSourceController {


    @Autowired
    private RuleDataSourceService ruleDataSourceService;


    @ApiOperation(value = "查询数据源配置列表", httpMethod = "POST",response = RuleDataSourceListVO.class)
    @HibasePostRequestMapping(value = "selectDataSourceList")
    public ResponseModel selectDataSourceList(@RequestBody RuleDataSourceSearchVO searchVO) {

        PageInfo pageInfo = ruleDataSourceService.selectDataSourceList(searchVO);
        pageInfo.setList(EntityUtil.transform(pageInfo.getList(), RuleDataSourceListVO.class));
        return ResponseModel.successMsg(pageInfo);

    }

    @ApiOperation(value = "新增数据源配置列表",httpMethod = "POST",response = ResponseModel.class)
    @HibasePostRequestMapping(value = "addDataSource")
    public ResponseModel addDataSource(@RequestBody RuleDataSourceInsertVO insertVO){

        return ResponseModel.successMsg(ruleDataSourceService.changeDataSource(EntityUtil.transform(insertVO, RuleDataSource.class)));

    }

    @ApiOperation(value = "编辑数据源配置列表",httpMethod = "POST",response = ResponseModel.class)
    @HibasePostRequestMapping(value = "updateDataSource")
    public ResponseModel updateDataSource(@RequestBody RuleDataSourceUpdateVO updateVO){

        return ResponseModel.successMsg(ruleDataSourceService.changeDataSource(EntityUtil.transform(updateVO, RuleDataSource.class)));

    }

    @ApiOperation(value = "删除数据源配置",httpMethod = "POST",response = ResponseModel.class)
    @HibasePostRequestMapping(value = "deleteDataSource")
    public ResponseModel deleteDataSource(@RequestBody OperationIdsEntity idsEntity){

        return ResponseModel.successMsg(ruleDataSourceService.deleteBy(idsEntity.getIds()));

    }

    @ApiOperation(value = "查看数据源详情",httpMethod = "POST",response = RuleDataSourceDetailVO.class)
    @HibasePostRequestMapping(value = "selectDetail")
    public ResponseModel selectDetail(@RequestBody OperationIdEntity idEntity){

        return ResponseModel.successMsg(EntityUtil.transform(ruleDataSourceService.getById(idEntity.getId()), RuleDataSourceDetailVO.class));

    }


}
